package com.itheima.controller;


import com.itheima.anno.RecordLogAnno;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {



    @Autowired
    private ClazzService clazzService;

    /**
     * 新增班级
     */
    @RecordLogAnno
    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        clazzService.save(clazz);
        return Result.success();
    }
    //3.1班级列表查询
    @RecordLogAnno
    @GetMapping
    public Result queryClassList(ClazzQueryParam clazzQueryParam){
        log.info("请求参数,{}", clazzQueryParam);
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    // 删除班级
    @RecordLogAnno
    @DeleteMapping("/{id}")
    public  Result  deleteClass(@PathVariable Integer id){
        clazzService. deleteClass(id);
        return Result.success();
    }

    //3.4 根据ID查询

    @RecordLogAnno
    @GetMapping("/{id}")
    public Result queryById (@PathVariable Integer id){
        Clazz clazz = clazzService.queryById(id);
        return Result.success(clazz);
    }

    //3.5 修改班级

    @PutMapping
    public Result modifyClass (@RequestBody Clazz clazz){
        clazzService.modifyClass(clazz);
        return  Result.success();
    }
    //3.6 查询所有班级
    @GetMapping("/list")
    public Result queryAllClasses(){
        List<Clazz> list = clazzService.queryAllClasses();
        return Result.success(list);
    }
}